<?php

namespace app\admin\model\advertiser;

use aliyun\AliyunOss;
use app\BaseModel;
use app\admin\model\base\User;
class AdvertiserSecondary extends BaseModel
{

    /**
     * 获取页面广告位列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @return array
     */
    public static function getList(array $where, int $page, int $limit, string $orderby = ''): array
    {
        $data = self::where($where)
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        $AliyunOss = new AliyunOss();
        //遍历数组
        foreach ($data['data'] as $k => &$v) {
            $v['img_url'] = $AliyunOss->getObjectUrl($v['img']);
        }
        return $data;
    }

    /**
     * 获取二次关注信息
     * @param int $user_id
     * @param string $third_id
     * @param string $mac_id
     * @return array
     */
    public static function getIdAccount(int $user_id,string $third_id, string $mac_id): array
    {
        //获取设置的二次关注信息
        $list = self::where('status','=','1')->whereIn('id',$third_id)->order('sort asc')->select()->toArray();
        //获取指定的路径设置
        if (!$list) {
            return [];
        }
        //获取用户信息
        $user = User::getIdUserInfo($user_id);
        if (!$user) {
            return [];
        }
        $data = [];
        $AliyunOss = new AliyunOss();
        //获取对应的号
        foreach ($list as $value){
            $res = AdvertiserSecondaryLog::itExist($user_id,$value['id']);
            if(!$res){
                //返回数据和写入取号日志
                AdvertiserSecondaryLog::AccountLog($user_id,$value['id'],$mac_id);
                $data = [
                    'url'=>$value['img'] ? $AliyunOss->getObjectUrl($value['img']) : '',
                    'type'=>$value['type'],
                    'name'=>$value['name']
                ];
                break;
            }
        }
        return $data;
    }
}